package fr.babos.graphic.components.medias 
{
	import flash.display.Shape;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.LocationChangeEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	import fr.babos.core.log.Log;
	import fr.babos.graphic.components.base.ResizableComponent;
	import fr.babos.graphic.components.webview.WebViewContainer;
	import fr.babos.graphic.errors.GraphicalError;
	import fr.babos.utils.ScreenUtils;
	import org.osflash.signals.Signal;
	
	/**
	 * Player video natif via un StageWebView.
	 * @author Pascal Achard / ZoulouX
	 */
	public class NativeVideoContainer extends WebViewContainer 
	{
		/**
		 * L'URL de la vidéo
		 */
		protected var _videoURL					:String;
		
		/**
		 * Démarrage automatique de la lecture
		 */
		protected var _autoPlay					:Boolean;
		
		/**
		 * Poster de la vidéo
		 */
		protected var _poster					:String;
		
		
		/**
		 * Constructeur
		 */
		public function NativeVideoContainer ()
		{
			// Styles du player
			_additionalStyle += <![CDATA[
				body {
					background-color: black;
				}
				#media
				{
					width: 100%;
					height: 100%;
				}
			]]>.toString();
			
			// Scripts du player
			_additionalScript += <![CDATA[
				var media;
				var mediaLoaded = false;
				var fullScreenState;
				
				function loadVid (pUrl, pAutoStart, pPoster)
				{
					createVideoElement(pUrl, pAutoStart, pPoster);
					
					if (pAutoStart)
						pauseOnFullScreenChange();
				}
				
				function pauseOnFullScreenChange ()
				{
					fullScreenState = media.webkitDisplayingFullscreen;
					
					window.setInterval(function () {
						if (media != null && fullScreenState != media.webkitDisplayingFullscreen)
						{
							fullScreenState = media.webkitDisplayingFullscreen;
							
							sendMessage("fullscreenStateChange", [fullScreenState]);
							
							if (!fullScreenState)
							{
								media.pause();
							}
						}
					}, 300);
				}
				
				function createVideoElement (pUrl, pAutoStart, pPoster)
				{
					if (media != null)
					{
						body.removeChild(media);
					}
					
					media = document.createElement("video");
					
					media.addEventListener("abort", mediaEvent);
					media.addEventListener("suspend", mediaEvent);
					media.addEventListener("canplaythrough", mediaEvent);
					media.addEventListener("emptied", mediaEvent);
					media.addEventListener("stalled", mediaEvent);
					media.addEventListener("error", mediaEvent);
					
					media.addEventListener("canplay", mediaCanPlay);
					
					media.addEventListener("ended", endedEvent);
					
					media.setAttribute("id", "media");
					
					media.setAttribute("controls", "controls");
					
					if (pAutoStart)
						media.setAttribute("preload", "preload");
					
					if (pPoster != null)
						media.setAttribute("poster", pPoster);
					
					media.setAttribute("src", pUrl);
					
					
					body.appendChild(media);
					
					if ("load" in media && pAutoStart)
					{
						media.load();
					}
					
					if ("play" in media && pAutoStart)
					{
						media.play();
					}
				}
				
				function mediaEvent (event)
				{
					sendMessage("mediaEvent", [event.type]);
				}
				
				function endedEvent (event)
				{
					sendMessage("mediaEnded", [event.type]);
					
					media.webkitExitFullscreen();
				}
				
				function mediaCanPlay (event)
				{
					sendMessage("mediaEvent", [event.type]);
					
					sendMessage("mediaLoaded", []);
					
					mediaLoaded = true;
				}
				
				function playCurrentVideo (pFullScreen)
				{
					pFullScreen = pFullScreen || true;
					
					if (media != null)
					{
						media.play();
						
						if (pFullScreen)
							media.webkitEnterFullscreen();
					}
				}
				
				function pauseCurrentVideo ()
				{
					if (media != null)
					{
						media.pause();
					}
				}
			]]>.toString();
		}
		
		/**
		 * Joue une vidéo
		 */
		public function loadVideo (pVideoURL:String, pAutoPlay:Boolean = true, pPoster:String = null):void 
		{
			// On stock l'URL
			_videoURL = pVideoURL;
			_autoPlay = pAutoPlay;
			_poster = pPoster;
			
			// Charger la vidéo si le player est prêt
			if (_ready)
			{
				callJavascript("loadVid", [pVideoURL, pAutoPlay, pPoster]);
			}
		}
		
		/**
		 * Le player est chargé
		 */
		override protected function completeHandler (event:Event):void 
		{
			// Relayer
			super.completeHandler(event);
			
			// Si on a un vidéo en attente
			if (_videoURL != "" && _videoURL != null)
			{
				// Charger la vidéo
				callJavascript("loadVid", [_videoURL, _autoPlay, _poster]);
			}
		}
	}
}